package com.tomorrow.runner.data.model;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.tomorrow.runner.data.AbsData;
import com.tomorrow.runner.data.DatabaseHelper;
import com.tomorrow.runner.data.RunProvider;

import java.util.HashSet;

import cn.sucun.android.utils.SQLUtility;

/**
 *
 * Created by tomorrow on 16-4-27.
 */
public class ActivityDataModel  extends AbsData {

    public static final String DATABASE = "activity_data.db";
    public static final int DATABASE_VERSION = 1;

    public static final String TABLE_NAME = "activity_data_info";
    public static final String CONTENT_NAME = "activity_data";

    public static final String ACTIVITY_ID = "activity_id";

    public static final String DATA_TYPE = "data_type";

    public static final String PRIMARY_DATA = "primary_data";

    public static final String SECONDARY_DATA = "secondary_data";

    public static final int TYPE_MILE = 0;

    public static final int TYPE_POSITION_POINT = 1;

    public static final int TYPE_CALORIE = 2;

    public static final int TYPE_ALTITUDE = 3;

    public static final int TYPE_PRESSURE = 4;

    public static final int TYPE_BASE_COUNT = 5;

    public static final int TYPE_AVG_SPEED = 6;

    protected static final HashSet<String> COLUMNS_INT;
    protected static final HashSet<String> COLUMNS_LONG;
    protected static final HashSet<String> COLUMNS_STR;
    protected static final HashSet<String> COLUMNS_BOOL;
    protected static final HashSet<String> COLUMNS_DOUBLE;

    static {
        COLUMNS_INT = new HashSet<>();
        COLUMNS_LONG = new HashSet<>();
        COLUMNS_STR = new HashSet<>();
        COLUMNS_BOOL = new HashSet<>();
        COLUMNS_DOUBLE = new HashSet<>();

        COLUMNS_INT.add(_ID);
        COLUMNS_INT.add(ACTIVITY_ID);
        COLUMNS_INT.add(DATA_TYPE);

        COLUMNS_STR.add(PRIMARY_DATA);
        COLUMNS_STR.add(SECONDARY_DATA);

    }
    public ActivityDataModel(Cursor c) {
        super(c, true, COLUMNS_STR, COLUMNS_INT, COLUMNS_LONG, COLUMNS_BOOL,COLUMNS_DOUBLE);
    }

    private static Uri sContentUri = null;
    /**
     * 获取数据库操作的uri
     * @return 数据库操作的uri
     */
    public static Uri getContentUri() {
        if (sContentUri == null) {
            sContentUri = Uri.withAppendedPath(RunProvider.getContentUri(),CONTENT_NAME);
        }
        return sContentUri;
    }
    @Override
    protected Uri getBaseUri() {
        return getContentUri();
    }

    /**
     * 通过帮助类继承SQLiteOpenHelper的帮助类对象 创建数据库
     */
    public static final DatabaseHelper.DBBuilder BUILDER = new DatabaseHelper.AbsDBBuilder(TABLE_NAME) {

        @Override
        public void createTable(Context context, SQLiteDatabase db) {
            StringBuilder sb = new StringBuilder();
            sb.append(_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
            sb.append(ACTIVITY_ID).append(" INTEGER NOT NULL, ");
            sb.append(DATA_TYPE).append(" INTEGER , ");

            sb.append(PRIMARY_DATA).append(" TEXT NOT NULL, ");
            sb.append(SECONDARY_DATA).append(" TEXT ");

            SQLUtility.createTable(db, TABLE_NAME, sb.toString());
        }

        @Override
        public boolean updateTable(SQLiteDatabase db, int oldVersion, int newVersion) {
            return newVersion > oldVersion;
        }
    };
}
